var api = require('../../utils/api.js')
var app = getApp()
Page({
    data: {
        code:'',
        openid:''
    },

    onLoad() {
  
    },
    onShow(){
        //调用登录接口
        var that=this;
        wx.login({
            success: function (ret) {
            console.log(ret);
            wx.getUserInfo({
                success: function () {
                    api.post({
                        url: 'wxapp/public/getopenid',
                        data: {'code':ret.code},
                        success: data => {
                            that.setData({
                                openid:data.data.openid
                            })
                        }
                    })
                }
            })

            that.setData({
                code:ret.code
            })
            }
        })




    },

    bindGetUserInfo: function(o) {
        var that=this;
        var openid=that.data.openid;
        var agentid=wx.getStorageSync('agentid');

        wx.getUserProfile({
            desc: '用于完善会员资料', 
            success: (res) => {
                var userInfo=JSON.parse(res.rawData);
    
                api.post({
                    url: 'wxapp/public/wxlogin',
                    data: {'openid':openid,'avatar':userInfo.avatarUrl,'nickName':userInfo.nickName,agentid:agentid},
                    success: data => {
                        var info=data.data;
                        console.log(info) 
                        wx.setStorageSync('user', info.user)
                        wx.setStorageSync('token', info.token)

                        wx.switchTab({
                          url: '/pages/my/my',
                        })

                    }
                })
            }
        })


    },
    goindex(){
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    getPhoneNumber: function(e){
        console.log(e.detail);
        var code=this.data.code;
        api.post({
            url: 'wxapp/getmobile',
            data: {'code':code},
            success: data => {
                console.log(data)
            }
        })
    },
})
